//
//  YSLScrollView.m
//  used
//
//  Created by lanou on 16/7/16.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "YSLScrollView.h"

#define KWidth self.frame.size.width
#define KHeight self.frame.size.height
@interface YSLScrollView ()<UIScrollViewDelegate>
//滚动视图
@property (nonatomic, strong)UIScrollView *scrollView;
//左图片
@property (nonatomic, strong)UIImageView *leftImageView;
//右图片
@property (nonatomic, strong)UIImageView *rightImageView;
//图片数组
@property (nonatomic, strong)NSArray *imageArray;
//当前下标
@property (nonatomic, assign)NSInteger index;
//小圆点
@property (nonatomic, strong)UIPageControl *pageControl;


@end

@implementation YSLScrollView

#pragma mark -------------------懒加载----------------------


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        //创建滚动视图及属性
        _scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        _scrollView.contentSize = CGSizeMake(KWidth * 2, KHeight);
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        //创建小圆点
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth/2 - 50, KHeight - 50, 100, 50)];
        _pageControl.numberOfPages = self.imageArray.count;
    }
    return _pageControl;
}

- (UIImageView *)leftImageView{
    
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithFrame:self.frame];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
        
    }
    return _leftImageView;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth, 0, KWidth, KHeight)];
        if (self.imageArray.count > 1 ) {
            
            [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
        }
    }
    return _rightImageView;
}


- (instancetype)initWithFrame:(CGRect)frame
                   imageArray:(NSArray *)imageArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = 1;
        //给数组赋值
        self.imageArray = imageArray;
        //设置视图
        [self createUI];
    }
    return self;
}

- (void)createUI{
    
    [self.scrollView addSubview:self.leftImageView];
    [self addSubview:self.scrollView];
    if (self.imageArray.count > 1) {
        [self.scrollView addSubview:self.rightImageView];
        [self addSubview:self.pageControl];
        
    }
    

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
        if (self.imageArray.count > 1) {
    
    if (scrollView.contentOffset.x == KWidth) {
        scrollView.contentOffset = CGPointZero;
        self.pageControl.currentPage = self.index;
        self.index++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
}
}

@end
